#! /usr/bin/env python
#coding=utf-8

import glob
import logging
import logging.handlers

LOGGINGLEVEL={
    'notset':logging.NOTSET,
    'debug':logging.DEBUG,
    'info':logging.INFO,
    'warning':logging.WARNING,
    'error':logging.ERROR,
    'critical':logging.CRITICAL
}
            

def initlog(argsdict):
    '''
    argsdict:传入参数的字典结构
    description:日志记录对象初始化
    '''
    #logger
    mylogger = logging.getLogger(argsdict['loggername'])
    mylogger.setLevel(argsdict['loglevel'])
    
    #handler
    handler = logging.handlers.RotatingFileHandler(argsdict['logfilename'],
                                                maxBytes=argsdict['maxbytes'],
                                                backupCount=argsdict['backupcount'])
    formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    handler.setFormatter(formatter)
        
    #add handler to logger
    mylogger.addHandler(handler)
    
    #return logger object
    return mylogger
    
def main():
    argsdict={
        'logfilename':'mylog.out',
        'loggername':'myapp',
        'loglevel':LOGGINGLEVEL['debug'],
        'maxbytes':300,
        'backupcount':5
    }
    mylogger=initlog(argsdict)
    
    #logging some information for test
    for i in range(30):
        mylogger.debug('app log information.\ti = %d' % i)
        mylogger.info('app log information.')
    
    logfiles = glob.glob('%s*' % argsdict['logfilename'])
    
    for filename in logfiles:
        print filename

if __name__ == '__main__':
    main()
    print 'done'
    